package com.cjy.runnable.demo10;

/**
 * @author Cheng JiYe
 * @description:
 * @date 2021/4/25 1:33
 */
public class MyThread implements Runnable {
  private int ticket = 5; // 假设一共有5张票

  public void run() {
    for (int i = 0; i < 100; i++) {
      this.sale(); // 调用同步方法
    }
  }

  /** 声明同步方法 */
  public synchronized void sale() {
    // 还有票
    if (ticket > 0) {
      try {
        // 加入延迟
        Thread.sleep(300);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
      System.out.println("卖票：ticket = " + ticket--);
    }
  }
}
